#include "stdio.h"
#include "string.h"

#include "esp_ota_ops.h"
#include "esp_chip_info.h"

#include "version.h"


/* 获取 Boot 版本信息 */
char *pcVersionBootloaderHardGet(void)
{
    esp_app_desc_t app_desc;
    static char datas[33] = {0};

    if(datas[0] != 0)
        return datas;

    esp_ota_get_partition_description(esp_ota_get_boot_partition(), &app_desc);

    memcpy(datas, app_desc.idf_ver, sizeof(datas));

    return datas;
}

char *pcVersionBootloaderSoftGet(void)
{
    esp_app_desc_t app_desc;
    static char datas[33] = {0};

    if(datas[0] != 0)
        return datas;

    esp_ota_get_partition_description(esp_ota_get_boot_partition(), &app_desc);

    memcpy(datas, app_desc.version, sizeof(datas));

    return datas;
}

char *pcVersionBootloaderDateGet(void)
{
    esp_app_desc_t app_desc;
    static char datas[33] = {0};

    if(datas[0] != 0)
        return datas;

    esp_ota_get_partition_description(esp_ota_get_boot_partition(), &app_desc);

    strncpy(datas,                 app_desc.date, sizeof(datas));
    datas[strlen(datas)] = ' ';
    strncpy(&datas[strlen(datas)], app_desc.time, sizeof(datas) - strlen(datas));

    return datas;
}


/* 获取 APP 版本信息 */
char *pcVersionAPPHardGet(void)
{
    esp_chip_info_t chip_info;
    static char datas[33] = {0};

    if(datas[0] != 0)
        return datas;

    esp_chip_info(&chip_info);

    sprintf(datas, "%d.%d", chip_info.revision / 100, chip_info.revision % 100);

    return datas;
}

char *pcVersionAPPSoftGet(void)
{
    esp_app_desc_t app_desc;
    static char datas[33] = {0};

    if(datas[0] != 0)
        return datas;

    esp_ota_get_partition_description(esp_ota_get_running_partition(), &app_desc);

    memcpy(datas, app_desc.version, sizeof(datas));

    return datas;
}

char *pcVersionAPPDateGet(void)
{
    esp_app_desc_t app_desc;
    static char datas[33] = {0};

    if(datas[0] != 0)
        return datas;

    esp_ota_get_partition_description(esp_ota_get_running_partition(), &app_desc);

    strncpy(datas,                 app_desc.date, sizeof(datas));
    datas[strlen(datas)] = ' ';
    strncpy(&datas[strlen(datas)], app_desc.time, sizeof(datas) - strlen(datas));

    return datas;
}
